<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/30
 * Time: 16:27
 */

namespace app\api\controller;

use app\pipeline\model\Business as BusinessModel;
use app\pipeline\model\Checklog as ChecklogModel;
use app\pipeline\model\Checklog;
use app\pipeline\model\Equipment as EquipmentModel;
use app\pipeline\model\Pipe as PipeModel;
use app\company\model\Company as CompanyModel;
use app\pipeline\model\SectionMeasuring as MeasuringModel;
use app\pipeline\model\PipeDevice as DeviceModel;
use app\pipeline\model\PipeSection as SectionModel;
use think\Controller;
use think\Db;
use think\Exception;

class Check extends Api
{
    public function initialize()
    {
        if (!$this->request->isPost()) {
            json_return('404 NOT FOUND');
        }
        //请求时添加默认添加请求日志
        $post_data=$this->request->post();
        $ip=$this->request->ip();
        if(!empty($post_data)){
            $post_data['Time']=date('Y-m-d H:i:s',time());
            $row_log=serialize($post_data);
            //写入日志
            $root_path = RUNTIME_PATH.'log';
            $time = date('ymd', time());
            $path = $root_path . "/checkpipeline/".$this->request->action()."/";
            $file_name = "$time.log";
            if (!is_dir($path)){
                $res = mkdir($path,0777,true);
                if (!$res) {
                    return false;
                }
            }
            if (!file_exists($path . $file_name)) {
                touch($path . $file_name);
            }
            $file = fopen($path . $file_name, "a");
            fwrite($file, $row_log . "\r\n");
            fwrite($file, "-------------------------------------------".$ip."-------------------------------------------------------------" . "\r\n");
            fclose($file);
        }
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /** 根据WGS坐标返回百度地图经纬度
     * request_type post
     * @parame string  longtitude 经度(转码前)
     * @parame string  latitude 纬度(转码前)
     * return json
     */
    public function gpstopoint(){
        $post_data=$this->request->post();
        $this->checkParam('longtitude,latitude');
        $x=unpack('d1double', strrev(hex2bin($post_data['longtitude'])))['double'];
        $y=unpack('d1double', strrev(hex2bin($post_data['latitude'])))['double'];
        $keys=config('keys.baidu_map_ac');
        $tude=$x.','.$y;
        $url="https://api.map.baidu.com/geoconv/v1/?coords=$tude&from=1&to=5&ak=$keys";
        $result = file_get_contents($url);
        $result=json_decode($result,true);
        if(isset($result['status'])&&$result['status']==0){
            $tude=array_pop($result['result']);
            $data=[
                'longtitud'=>$tude['x'],
                'latitude'=>$tude['y']
            ];
            json_return('获取地图坐标系成功',true,$data);
        }else{
            json_return('获取地图坐标系失败',false);
        }
    }

    /**
     * 根据设备信息返回业务,设备,管道信息
     * request_type post
     * @parame string  equipment_number 设备编号
     * return json
     */
    public function getbusiness(){
        $this->checkParam('equipment_number');
        $post_data=$this->request->post();
        $equipment_id=EquipmentModel::where('number',$post_data['equipment_number'])->value('id');
        if(!$equipment_id){
            json_return('未找到设备的相关信息',false);
        }
        $business_info=BusinessModel::where("equipment_number",$equipment_id)->order('id','desc')->find();
        $return_data=[];
        if($business_info){
            $return_data['euipment_info']=[
                'equipment_name'=>EquipmentModel::where("id",$business_info['equipment_number'])->value('name'),
                'equipment_number'=>$post_data['equipment_number']
            ];
            $company_info=CompanyModel::field('id,name')->whereIn('id',$business_info['pipeline_company'])->select();
            $pipe_infos=PipeModel::alias('p')
                ->field('p.id as pipe_id,p.name,p.number as pipeline_number,p.company_id,c.name as company_name')
                ->leftjoin('company c','p.company_id = c.id')
                ->whereIn('p.id',$business_info['pipeline_number'])
                ->select()->toArray();

            foreach($company_info as $val){
                $data=[];
                foreach ($pipe_infos as $v){
                    if($v['company_id']==$val['id']){
                        $data[]=$v;
                    }
                }
                $val['data']=$data;
            }
            $return_data['pipeline_info']=$company_info;
            $return_data['business_info']=$business_info;
            json_return('获取当前设备最新业务信息成功',true,$return_data);
        }
        json_return('未找到业务相关信息',false);
    }

    /**
     * 业务添加管道
     * request_type post
     * @param  int  business_id 业务ID
     * @param string  name 管道名称
     * @param string  pipeline_number 管道编号
     * @param string company_id 管道所属公司ID逗号连接
     * return json
     */
    public function insertpipe(){
        $post_data=$this->request->post();
        $this->checkParam('business_id,name,pipeline_number,company_id');
        $pipe_numbers=explode(',',$post_data['pipeline_number']);
        $pipe_name=explode(',',$post_data['name']);
        $pipe_company=explode(',',$post_data['company_id']);
        if(count($pipe_numbers)!==count($pipe_name)){
            json_return('缺少管道编号或名称',false);
        }
        if(count($pipe_numbers)!==count($pipe_company)){
            json_return('缺少管道名称或管道所属公司ID',false);
        }
        $business_info=BusinessModel::where('id',$post_data['business_id'])->find();
        if(!$business_info){
            json_return('未找到业务信息',false);
        }
        $company_info=CompanyModel::field('company_type,id')->where('id',$business_info['company_id'])->find();
        //判断添加管道所属的公司是否正确
        if($company_info['company_type']=='third'){
            $has_company=CompanyModel::where('b_company',$business_info['company_id'])->whereIn('id',$pipe_company)
                ->column('id');
            if(count($has_company)!==count($pipe_company)){
                $has_company=array_values($has_company);
                $diff_company=array_diff($pipe_company,$has_company);
                json_return('当前第三方业务,不存在客户公司ID:'.implode(',',$diff_company).',请重新输入',false);
            }
        }else{
            $diff_company=array_diff($pipe_company,[$company_info['id']]);
            if($diff_company){
                json_return('当前业务所属的公司,不存在公司ID:'.implode(',',$diff_company).',请重新输入',false);
            }
        }

        if(!empty($business_info['pipeline_number'])){
            $business_info['pipeline_number']=explode(',',$business_info['pipeline_number']);
        }
        $pipe_ids=PipeModel::whereIn("number",$pipe_numbers)->column('id');

        $in_pipeids=[];
        if(empty($pipe_ids)){
            //一个没有全部添加
            foreach($pipe_numbers as $k=>$val){
                $pipe_data=[];
                $pipe_data['name']=$pipe_name[$k];
                $pipe_data['number']=$val;
                $pipe_data['company_id']=$pipe_company[$k];
                $pipe_id=PipeModel::create($pipe_data);
                if(!$pipe_id){
                    json_return('管道添加失败,请重试',false);
                }
                $in_pipeids[]=$pipe_id['id'];
            }
            $diffs=array_diff($in_pipeids,$business_info['pipeline_number']);
        }else{
            $error_pipe=[];
            //存在已有管道和没有添加的
            foreach($pipe_numbers as $k=>$val){
                $has_pipe=PipeModel::where('number',$val)->value('company_id');
                if(!$has_pipe){
                    $pipe_data['name']=$pipe_name[$k];
                    $pipe_data['number']=$val;
                    $pipe_data['company_id']=$pipe_company[$k];
                    $pipe_id=PipeModel::create($pipe_data);
                    $in_pipeids[]=$pipe_id['id'];
                }else{
                    //判断已存在的管道编号所属权是否正确
                    if(!in_array($has_pipe,$pipe_company)){
                        $error_pipe[]=$val;
                    }
                }
            }

            if(!empty($error_pipe)){
                json_return('管道编号'.implode(',',$error_pipe).'不属于当前业务公司,无法添加');
            }
            $pipe_ids=array_merge($in_pipeids,$pipe_ids);
            $diffs=array_diff($pipe_ids,$business_info['pipeline_number']);
        }

        if(!empty($diffs)){
            $new_pipeline_ids=array_merge($diffs,$business_info['pipeline_number']);
            sort($new_pipeline_ids);
            $new_pipeline_ids=implode(',',$new_pipeline_ids);
            $result=BusinessModel::where('id',$post_data['business_id'])->update(['pipeline_number'=>$new_pipeline_ids]);
            if(!$result){
                json_return('修改业务管道信息失败',false);
            }
            $msg='修改业务管道信息成功';
        }else{
            $msg='业务已存在管道,不需要更新';
        }

        $pipe_info=PipeModel::whereIn("id",$business_info['pipeline_number'])->field('name,number')->select()->toArray();
        $business_info['pipeline_name']=implode(',',array_column($pipe_info,'name'));
        $business_info['pipeline_number']=implode(',',array_column($pipe_info,'number'));
        $equipment_info=EquipmentModel::where("id",$business_info['equipment_number'])->field('name,number')->find();
        $business_info['equipment_name']=$equipment_info['name'];
        $business_info['equipment_number']=$equipment_info['number'];
        $business_info['company_name']=CompanyModel::where('id',$business_info['company_id'])->value('name');
        json_return($msg,true,$business_info);
    }

    /**
     * 保存检测记录
     * request_type post
     * @param int business_id  业务ID
     * @param string pipeline_number 管道编号
     * @param int company_id  管道所属公司ID
     * @param string longtitude 经度
     * @param string latitude 纬度
     * @param string deep  埋深
     * @param float(int) thickness 厚度
     * @param string signcode 签名
     * return json
     */
    public function upcheck(){
        $post_data=$this->request->post();
        $this->checkParam('business_id,pipeline_number,company_id,longtitude,latitude');

        //验证获取参数
        //验证管道信息
        $pipe_where=[];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_info=PipeModel::field('id,company_id')->where($pipe_where)->find();
        if(!$pipe_info){
            json_return('未找到管道信息',false);
        }

        //验证业务信息
        $business_info=BusinessModel::field('company_id,status,pipeline_company')->where('id',$post_data['business_id'])->find();
        if(empty($business_info['company_id'])) {
            json_return('未找到业务所属公司', false);
        }
        if($business_info['status']==2){
            json_return('当前业务已结束无法添加记录',false);
        }
        //管道非当前业务公司所属或第三方所属
        if(!in_array($pipe_info['company_id'],explode(',',$business_info['pipeline_company']))){
            json_return('业务信息错误无法添加',false);
        }

        //添加参数
        $check_data=[];
        $check_data['company_id']=$pipe_info['company_id'];
        $check_data['business_id']=$post_data['business_id'];
        $check_data['pipeline_number']=$pipe_info['id'];
        $check_data['longtitude']=$post_data['longtitude'];
        $check_data['latitude']=$post_data['latitude'];
        $check_data['center']=$post_data['longtitude'].','.$post_data['latitude'];
        $check_data['deep']=!empty($post_data['deep'])?$post_data['deep']:0;
        $check_data['thickness']=!empty($post_data['thickness'])?$post_data['thickness']:0;
        $check_data['create_time']=date('Y-m-d H:i:s');
        $where=[];
        $where['business_id']=$post_data['business_id'];
        $where['pipeline_number']=$pipe_info['id'];
        $where['company_id']=$post_data['company_id'];
        //存在两次检测点为同一条数据情况,以点位序号判断是更新还是新增
        if(!empty($post_data['sort'])){
            $where['sort']=$post_data['sort'];
            $check_data['update_time']=date('Y-m-d H:i:s');
            $row_id=ChecklogModel::where($where)->value('id');
            if(!$row_id){
                json_return('添加记录点失败,未找到指定序号',false);
            }

            $result=ChecklogModel::where('id',$row_id)->update($check_data);
            if(!$result){
                json_return('添加记录点失败',false);
            }
            $check_data['id']=$row_id;
            $check_res=$check_data;

        }else{
            $sort=ChecklogModel::where($where)->order('id','desc')->value('sort');
            $check_data['sort']=$sort>0?$sort+1:1;
            $check_res=ChecklogModel::create($check_data);
            if(!$check_res){
                json_return('添加记录点失败',false);
            }
        }

        //修改业务状态为进行中
        if($business_info['status']==0){
            BusinessModel::where('id',$post_data['business_id'])->update(['status'=>1]);
        }
        $check_res['signcode']=!empty($post_data['signcode'])?$post_data['signcode']:'';
        $check_res['pipeline_number']=$post_data['pipeline_number'];
        json_return('添加记录点成功',true,$check_res);
    }

    /**
     * 修改监测点信息
     * request_type post
     * @param  int business_id  业务ID
     * @param  string pipeline_number 管道编号
     * @param  int company_id   管道所属公司ID
     * @param  int sort 检测点序号
     * @param  string longtitude 检测点经度
     * @param  string latitude 检测点纬度
     * return json
     */
    public function changepoint(){
        $post_data=$this->checkParam('business_id,pipeline_number,company_id,sort,longtitude,latitude');
        //验证获取参数
        //验证管道
        $pipe_where=[];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_info=PipeModel::field('company_id,id')->where($pipe_where)->find();
        if(!$pipe_info){
            json_return('未找到管道信息',false);
        }
        //验证业务信息
        $business_info=BusinessModel::field('company_id,status')->where('id',$post_data['business_id'])->find();
        if(empty($business_info['company_id'])) {
            json_return('未找到业务所属公司', false);
        }
        if($business_info['status']==2){
            json_return('当前业务已结束无法修改检测点',false);
        }
        //验证检测点信息
        $where=[];
        $where['business_id']=$post_data['business_id'];
        $where['sort']=$post_data['sort'];
        $where['pipeline_number']=$pipe_info['id'];
        $where['company_id']=$post_data['company_id'];
        $checklog_info=ChecklogModel::where($where)->find();
        if(!$checklog_info){
            json_return('未找到检测点信息',false);
        }
        if($checklog_info['longtitude']!=$post_data['longtitude']&&$checklog_info['latitude']!=$post_data['latitude']){
            $updata=[];
            $updata['longtitude']=$post_data['longtitude'];
            $updata['latitude']=$post_data['latitude'];
            $updata['center']=$post_data['longtitude'].','.$post_data['latitude'];
            $updata['update_time']=date('Y-m-d H:i:s');
            $update_res=ChecklogModel::where($where)->update($updata);
            if(!$update_res){
                json_return('修改检测点失败',false);
            }
            $checklog_info['longtitude']=$post_data['longtitude'];
            $checklog_info['latitude']=$post_data['latitude'];
        }
        json_return('修改监测点成功,获取修改后的检测点',true,$checklog_info);
    }

    /**
     * 插入监测点并重新排序
     * request_type post
     * @param  int business_id  业务ID
     * @param  int company_id  管道所属公司ID
     * @param  string pipeline_number 管道编号
     * @param  int prev_sort 插入检测点的前一位序号
     * @param  int sort 检测点序号
     * @param  string longtitude 检测点经度
     * @param  string latitude 检测点纬度
     */
    public function insertpoint(){
        $post_data=$this->checkParam('business_id,company_id,pipeline_number,prev_sort,longtitude,latitude');
        if(!is_numeric($post_data['prev_sort'])||$post_data['prev_sort']<0){
            json_return('检测点参数错误',false);
        }
        $pipe_where=[];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_info=PipeModel::field('company_id,id')->where($pipe_where)->find();
        if(!$pipe_info){
            json_return('未找到管道信息',false);
        }
        //验证业务信息
        $business_info=BusinessModel::field('company_id,status')->where('id',$post_data['business_id'])->find();
        if(empty($business_info['company_id'])) {
            json_return('未找到业务所属公司', false);
        }
        if($business_info['status']==2){
            json_return('当前业务已结束无法插入检测点',false);
        }
        //验证检测点信息
        $where=[];
        $where['business_id']=$post_data['business_id'];
        $where['pipeline_number']=$pipe_info['id'];
        $where['company_id']=$post_data['company_id'];
        $all_checkinfo=ChecklogModel::field('id,sort')->where($where)->order('sort asc')->select()->toArray();
        $all_sort=array_column($all_checkinfo,'sort');

        //前一个点为零时跳过判断
        if(!in_array($post_data['prev_sort'],$all_sort)&&$post_data['prev_sort']>0){
            json_return('未找到插入前检测点信息',false);
        }

        $after_checkinfo=[];
        foreach($all_checkinfo as $val){
            if($val['sort']>$post_data['prev_sort']){
                $val['sort']+=1;
                $val['update_time']=date('Y-m-d H:i:s');
                $after_checkinfo[]=$val;
            }
        }

        Db::startTrans();
        try{
            $checklog_model=new ChecklogModel();
            $update_res=$checklog_model->saveAll($after_checkinfo);
            if(!$update_res){
                Db::rollback();
                json_return('修改排序失败',false);
            }
            $insert_data=[];
            $insert_data['company_id']=$pipe_info['company_id'];
            $insert_data['business_id']=$post_data['business_id'];
            $insert_data['pipeline_number']=$pipe_info['id'];
            $insert_data['sort']=$post_data['prev_sort']+1;
            $insert_data['longtitude']=$post_data['longtitude'];
            $insert_data['latitude']=$post_data['latitude'];
            $insert_data['center']=$post_data['longtitude'].','.$post_data['latitude'];
            $insert_data['deep']=!empty($post_data['deep'])?$post_data['deep']:0;
            $insert_data['thickness']=!empty($post_data['thickness'])?$post_data['thickness']:0;
            $insert_data['create_time']=date('Y-m-d H:i:s');
            $insert_res=Checklog::insert($insert_data);
            if(!$insert_res){
                Db::rollback();
                json_return('插入监测点失败',false);
            }
            Db::commit();
            json_return('插入检测点重新排序成功',true);
        }catch(Exception $e){
            Db::rollback();
            json_return('修改排序失败'.$e->getMessage(),false);
        }
    }

    /**
     * 删除指定检测点
     * request_type post
     * @param int business_id 业务ID
     * @param int company_id 管道所属公司ID
     * @param string pipeline_number 管道编号
     * @param int sort 要删除的检测点序号
     * @throws \Exception
     * return json
     */
    public function delpoint(){
        $post_data=$this->checkParam('business_id,company_id,pipeline_number,sort');
        $pipe_where=[];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_info=PipeModel::field('company_id,id')->where($pipe_where)->find();
        if(!$pipe_info){
            json_return('未找到管道信息',false);
        }
        //验证业务信息
        $business_info=BusinessModel::where('id',$post_data['business_id'])->value('id');
        if(!$business_info) {
            json_return('未找到业务所属公司', false);
        }
        //验证检测点信息
        $where=[];
        $where['business_id']=$post_data['business_id'];
        $where['sort']=$post_data['sort'];
        $where['pipeline_number']=$pipe_info['id'];
        $where['company_id']=$post_data['company_id'];
        $row_data=ChecklogModel::where($where)->lock('lock in share mode')->find();
        if(!$row_data){
            json_return('未找到相关检测点',false);
        }

        Db::startTrans();
        try{
            $res=ChecklogModel::where("id",$row_data['id'])->delete();
            if(!$res){
                Db::rollback();
                json_return('删除失败',false);
            }
            //删除检测点后重新排序
            $where=[];
            $where['business_id']=$row_data['business_id'];
            $where['company_id']=$row_data['company_id'];
            $where['pipeline_number']=$row_data['pipeline_number'];
            $ori_sort=$row_data['sort'];
            $ids=ChecklogModel::where($where)->where('sort','>',$row_data['sort'])->order('sort asc')->column('id');
            if(!empty($ids)){
                $new_sort=[];
                for($i=0;$i<=count($ids)-1;$i++){
                    $row=[];
                    $row['id']=$ids[$i];
                    $row['sort']=$ori_sort;
                    $row['update_time']=date('Y-m-d H:i:s');
                    $new_sort[]=$row;
                    $ori_sort++;
                }
                if(!empty($new_sort)){
                    $chelogmodel=new ChecklogModel();
                    $res=$chelogmodel->saveAll($new_sort,true);
                    if(!$res){
                        Db::rollback();
                        json_return('删除排序失败',false);
                    }
                    Db::commit();
                    json_return('删除成功');
                }
            }else{
                Db::commit();
                json_return('删除成功');
            }
        }catch(Exception $e){
            Db::rollback();
            json_return('删除失败',false);
        }

    }

    /**
     * 根据业务信息获取检测点
     * request_type post
     * @param int business_id 业务ID
     * @param int company_id 管道所属公司ID
     * @param string pipeline_number 管道编号
     * return json
     */
    public function getrowpoint(){
        $post_data=$this->checkParam('business_id,company_id,pipeline_number');
        $pipe_where=[];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_id=PipeModel::where($pipe_where)->value('id');
        if(!$pipe_id){
            json_return('未找到相关管道',false);
        }
        $where=[];
        $where['business_id']=$post_data['business_id'];
        $where['company_id']=$post_data['company_id'];
        $where['pipeline_number']=$pipe_id;
        $sort_list=ChecklogModel::where($where)->order('sort asc')->select();
        if(!$sort_list){
            json_return('为找到相关检测点信息',false);
        }
        $pipeline_ennumber='';
        $company_name='';
        foreach($sort_list as$key=>$val){
            if(empty($company_name)&&empty($pipeline_ennumber)){
                $company_name=CompanyModel::where('id',$val['company_id'])->value('name');
                $pipeline_ennumber=PipeModel::where('id',$val['pipeline_number'])->value('number');
            }
            $sort_list[$key]['company_name']=$company_name;
            $sort_list[$key]['pipeline_number']=$pipeline_ennumber;
        }

        json_return('获取业务信息成功',true,$sort_list);
    }


    /**
     * 上传gps检测点下的管段测点信息
     * request_type post
     * @param string section_name 管段名称
     * @param string pipe_diameter 设备名称
     * @param int company_id 管道所属公司ID
     * @param string pipeline_number 管道编号
     * @param int business_id 业务ID
     * @param int sort GPS检测点序号
     * @param string|int pipe_diameter 管径
     * @param string|int buried_depth 埋深
     * @param array|int measuring_point 测点
     * return json
     */
    public function upmeasuringpoint(){
        $this->checkParam('business_id,company_id,pipeline_number,sort,section_name,device_name');
        $post_data=$this->request->post();
        //数据验证
        $pipe_where=[];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_id=PipeModel::where($pipe_where)->value('id');
        if(!$pipe_id){
            json_return('未找到名为'.$post_data['pipeline_number'].'的管道信息',false);
        }
        $log_where=[];
        $log_where['business_id']=$post_data['business_id'];
        $log_where['company_id']=$post_data['company_id'];
        $log_where['pipeline_number']=$pipe_id;
        $log_where['sort']=$post_data['sort'];
        $check_info=ChecklogModel::where($log_where)->value('id');
        if(!$check_info){
            json_return('未找到GPS检测点'.$post_data['sort'].'的相关信息',false);
        }
        $section_id=SectionModel::where('section_name',$post_data['section_name'])->value('id');
        if(!$section_id){
            json_return('为找到名为'.$post_data['section_name'].'的管段信息',false);
        }
        $device_id=DeviceModel::where('device_name',$post_data['device_name'])->value('id');
        if(!$device_id){
            json_return('为找到名为'.$post_data['device_name'].'的装置信息',false);
        }
        $measuring_where=[];
        $measuring_where['business_id']=$post_data['business_id'];
        $measuring_where['company_id']=$post_data['company_id'];
        $measuring_where['pipeline_number']=$pipe_id;
        $measuring_where['sort']=$post_data['sort'];
        $measuring_id=MeasuringModel::where($measuring_where)->value('id');
        if($measuring_id){
            json_return('GPS坐标点'.$post_data['sort'].'的测点信息已录入,请勿重复录入',false);
        }

        //拼接参数
        $insert_data=[];
        $insert_data['business_id']=$post_data['business_id'];
        $insert_data['company_id']=$post_data['company_id'];
        $insert_data['sort']=$post_data['sort'];
        $insert_data['pipeline_number']=$pipe_id;
        $insert_data['section_id']=$section_id;
        $insert_data['device_id']=$device_id;
        $insert_data['pipe_diameter']=!empty($post_data['pipe_diameter'])?$post_data['pipe_diameter']:'';
        $insert_data['buried_depth']=!empty($post_data['buried_depth'])?$post_data['buried_depth']:'';
        $insert_data['measuring_point']=!empty($post_data['measuring_point'])?implode(',',(array)$post_data['measuring_point']):'';
        $insert_data['create_time']=date('Y-m-d H:i:s');
        $insert_res=MeasuringModel::create($insert_data);
        if(!$insert_res){
            json_return('上传测点数据失败',false);
        }
        $insert_res['pipeline_number']=$post_data['pipeline_number'];
        $insert_res['section_name']=$post_data['section_name'];
        $insert_res['device_name']=$post_data['device_name'];
        json_return('上传测点数据成功',true,$insert_res);
    }

    /**
     * 获取GPS坐标点下的测点信息
     * request_type post
     * @param int business_id 业务ID
     * @param int company_id 管道所属公司ID
     * @param string pipeline_number 管道编号
     * @param int sort GPS检测点序号
     * return json
     */
    public function getmeasuringpoint(){
        $this->checkParam('business_id,company_id,pipeline_number,sort');
        $post_data=$this->request->post();
        $pipe_where=[];
        $pipe_where['company_id']=$post_data['company_id'];
        $pipe_where['number']=$post_data['pipeline_number'];
        $pipe_info=PipeModel::field('id,number')->where($pipe_where)->find();
        if(!$pipe_info){
            json_return('未找到名为'.$post_data['pipeline_number'].'的管道信息',false);
        }
        $where=[];
        $where['business_id']=$post_data['business_id'];
        $where['company_id']=$post_data['company_id'];
        $where['pipeline_number']=$pipe_info['id'];
        $where['sort']=$post_data['sort'];
        $measuring_info=MeasuringModel::where($where)->find();
        if(!$measuring_info){
            json_return('未找到GPS检测点'.$post_data['sort'].'的相关管段测点信息',false);
        }
        $measuring_info['company_name']=CompanyModel::where('id',$measuring_info['company_id'])->value('name');
        $measuring_info['pipeline_number']=$pipe_info['number'];
        $measuring_info['section_name']=SectionModel::where('id',$measuring_info['section_id'])->value('section_name');
        $measuring_info['device_name']=DeviceModel::where('id',$measuring_info['device_id'])->value('device_name');
        json_return('获取GPS检测点'.$post_data['sort'].'下的测点信息成功',true,$measuring_info);
    }





    /**
     * 结束当前业务
     * request_type post
     * @param int business_id 业务ID
     */
//    public function offbusiness(){
//        $this->checkParam('business_id');
//        $post_data=$this->request->post();
//        $res=BusinessModel::where('id',$post_data['business_id'])->update(['status'=>2]);
//        if(!$res){
//            json_return('结束任务失败,请重试',false);
//        }
//        json_return('结束任务成功');
//    }






}